package com.tpo.jnotes.client.layer.view.components;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

import com.tpo.jnotes.base.colors.schema.BCSSManager;
import com.tpo.jnotes.client.layer.view.dialogs.NBaseFrame;
import com.tpo.jnotes.client.lib.component.optionpane.NCustomOptionPane;
import com.tpo.jnotes.client.lib.util.EventsUtils;

public class NTitleEditor extends JLabel implements MouseListener
{

    private final static int TITLE_MAX_LENGTH = 15;
    private final static int TITLE_POINTS_LENGTH = 3;

    private String displayText;
    private String fullText;
    private final NBaseFrame frame;

    public NTitleEditor( NBaseFrame aFrame )
    {
        this.frame = aFrame;
        setOpaque( false );
        setHorizontalAlignment( SwingConstants.CENTER );
        setFont( BCSSManager.getCurrentCSS().getTitleFont() );
        setForeground( BCSSManager.getCurrentCSS().getTitleForeground() );
        setBackground( BCSSManager.getCurrentCSS().getTitleBackground() );
        addMouseListener( this );
    }

    @Override
    public void setText( String text )
    {
        if( (text != null) && (text.length() > TITLE_MAX_LENGTH + TITLE_POINTS_LENGTH) )
        {
            displayText = text.substring( 0, TITLE_MAX_LENGTH ) + "...";
            fullText = text;
        }
        else
        {
            displayText = text;
            fullText = text;
        }
        super.setText( displayText );
        setToolTipText( fullText );
    }

    @Override
    public void mouseClicked( MouseEvent event )
    {
        if( EventsUtils.isDoubleClick( event ) )
        {
            String newTitle = NCustomOptionPane.showInputDialog( frame, 0, "New title", fullText );
            if( newTitle != null )
                setText( newTitle );
        }
    }

    @Override
    public void mousePressed( MouseEvent e )
    {
    }

    @Override
    public void mouseReleased( MouseEvent e )
    {
    }

    @Override
    public void mouseEntered( MouseEvent e )
    {
    }

    @Override
    public void mouseExited( MouseEvent e )
    {
    }

}
